package com.zhangh.summer.admin.support.prometheus.redis;

import com.zhangh.summer.admin.support.ds.common.MixedDSImpl;
import com.zhangh.summer.admin.support.ds.redis.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 连接池收集器
 * @author: zhanghui  zhanghlucky@163.com
 * @create: 2020-06-29 16:09
 **/
@Component
@Slf4j
public class RedisCollector {
    private Map<String, Pool<Jedis>> jedisPollMap=new HashMap<String, Pool<Jedis>>();
    private Map<String,String> maxTotalMap=new HashMap<String, String>();
    @Resource
    private MixedDSImpl mixedDS;

    @PostConstruct
    public void init(){

    }

    public void execute(){

        Map<String, RedisClient>  map=mixedDS.getRedisClientMap();
        map.forEach((key, value) -> {
            Jedis jedis=null;
            try {
                jedis=value.getResource();
                String host =jedis.getClient().getHost();
                int port =jedis.getClient().getPort();
                long db=jedis.getClient().getDB();
                String url=host+":"+port+"/"+db;
                jedisPollMap.put(url,value.getPool());
                Field field= null;
                try {
                    field = Pool.class.getDeclaredField("internalPool");
                    field.setAccessible(true);
                    GenericObjectPool internalPool=(GenericObjectPool) field.get(value.getPool());
                    maxTotalMap.put(url, internalPool.getMaxTotal()+"");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }catch (Exception e){
                log.error("get redis error:{}",e.getMessage());
                e.printStackTrace();
            }finally {
                if (null!=jedis){
                    jedis.close();
                }
            }
        });

        refresh();
    }

    private void refresh() {
        for(String url:jedisPollMap.keySet()){
            Pool<Jedis>jedisPool=jedisPollMap.get(url);
            String maxTotal=maxTotalMap.get(url);
            RedisMetrics.maxBorrowWaitTimeMillisEndpoint.labels(url).set((double) jedisPool.getMaxBorrowWaitTimeMillis());
            RedisMetrics.meanBorrowWaitTimeMillisEndpoint.labels(url).set((double) jedisPool.getMeanBorrowWaitTimeMillis());
            RedisMetrics.numActiveEndpoint.labels(url).set((double) jedisPool.getNumActive());
            RedisMetrics.numIdleEndpoint.labels(url).set((double) jedisPool.getNumIdle());
            RedisMetrics.numWaitersEndpoint.labels(url).set((double) jedisPool.getNumWaiters());
            RedisMetrics.maxTotalEndpoint.labels(url).set(Double.valueOf(maxTotal));
        }
    }


}
